import 'dart:async';

import 'package:amoy_ticket/utils/router/getRouteConfig.dart';
import 'package:event_bus/event_bus.dart';
import 'package:get/get.dart';
import 'package:jpush_flutter/jpush_flutter.dart';

import '../../controller/event/event_declration.dart';
import '../../pages/common/loading_page.dart';

class SplashLogic extends GetxController {
  int count = 5;
  late Timer timer;
  bool remove = true;
  late StreamSubscription _streamLoading;
  @override
  void onInit() {
    eventBus = EventBus();
    _streamLoading = initLoadingDialog(Get.context);

    startJPush();
    countDown();
    super.onInit();
  }

  @override
  void dispose() {
    _streamLoading.cancel();
    super.dispose();
  }

  void countDown() {
    timer = Timer.periodic(Duration(milliseconds: 1000), (timer) {
      count--;
      if (count == 0) {
        timer.cancel();

        toNextPage();
      }
      update();
    });
  }

  void toNextPage() {
    if (remove) {
      Get.offAndToNamed(RouteConfig.tabs);
      remove = false;
      update();
    }
  }

  ///初始化极光推送
  void startJPush() {
    JPush jpush = JPush();
    //配置jpush(不要省略）
    //debug就填debug:true，生产环境production:true
    jpush.setup(
        appKey: 'e8a9b47b818c9ae6c1b6e25c',
        channel: 'developer-default',
        production: true,
        debug: true);
    //监听jpush(ios必须配置)
    jpush.applyPushAuthority(
        const NotificationSettingsIOS(sound: true, alert: true, badge: true));
    jpush.addEventHandler(
      onReceiveNotification: (Map<String, dynamic> message) async {
        print('message11:$message');
      },
      onOpenNotification: (Map<String, dynamic> message) async {
        //点击通知栏消息，在此时通常可以做一些页面跳转等
        print('message22:$message');
      },
    );
  }
}
